/*
* 文件：AndroidTool.cs
* 作者：王阳
* 时间：2019-5-20
* 描述：AndroidTool
*/

//#define ENABLED_CONSUMES_OUTSIDE_TOUCHES

using System.IO;
using UnityEngine;

namespace UnityX
{
    public static class AndroidTool
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        private static AndroidJavaObject currentActivity;
        private static AndroidJavaClass androidTool;
        static AndroidTool()
        {
            AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            androidTool = new AndroidJavaClass("com.htdw.unityx.AndroidTool");
        }
#if ENABLED_CONSUMES_OUTSIDE_TOUCHES
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
        private static void AfterSceneLoad()
        {
            TouchScreenKeyboard.Android.consumesOutsideTouches = false;//指示键盘是否在可见键盘区域之外使用屏幕触摸
        }
#endif
#endif
        /// <summary>
        /// 相册目录
        /// </summary>
        public static string DCIMFolder
        {
            get
            {
                string root = AndroidRootFolder;
                if (Directory.Exists(root + "/DCIM/Camera"))
                    return root + "/DCIM/Camera";
                if (Directory.Exists(root + "/DCIM/Screenshots"))
                    return root + "/DCIM/Screenshots";
                return root + "/DCIM";
            }
        }
        /// <summary>
        /// 安卓跟目录
        /// </summary>
        public static string AndroidRootFolder
        {
            get
            {
                return Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
            }
        }

        /// <summary>
        /// 刷新文件
        /// </summary>
        /// <param name="path"></param>
        public static void ScanFile(string path)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
        string[] paths = new string[] { path };
        using (AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            using (AndroidJavaClass MediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))
            {
                MediaScannerConnection.CallStatic("scanFile", currentActivity, paths, null, null);
            }
        }
#endif
        }

        /// <summary>
        /// 获取手机号
        /// </summary>
        /// <returns></returns>
        public static string GetPhoneNumber()
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            return androidTool.CallStatic<string>("GetPhoneNumber", currentActivity);
#else
            return null;
#endif
        }

        /// <summary>
        /// 震动
        /// </summary>
        /// <param name="duration">持续时间，毫米为单位</param>
        public static void Vibrate(long duration)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
        androidTool.CallStatic("Vibrate", currentActivity, duration);
#endif
        }
        /// <summary>
        /// 震动
        /// </summary>
        /// <param name="delay">延迟启动，毫米为单位</param>
        /// <param name="duration">持续时间，毫米为单位</param>
        public static void Vibrate(long delay, long duration)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
        androidTool.CallStatic("Vibrate", currentActivity, new long[] { delay, duration }, -1);
#endif
        }
        /// <summary>
        /// 震动
        /// </summary>
        /// <param name="mpattern">以毫秒为单位打开或关闭振动器的持续时间。第一个值表示在打开振动器之前要等待的毫秒数。下一个值表示在关闭振动器之前保持振动器的毫秒数。随后的值在以毫秒为单位的持续时间之间交替，以关闭振动器或打开振动器</param>
        /// <param name="repeat">重复，-1为不重复</param>
        public static void Vibrate(long[] mpattern, int repeat)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
        androidTool.CallStatic("Vibrate", currentActivity, mpattern, repeat);
#endif
        }
    }
}
